//
//  HJZStatusFrame.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/6/1.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "HJZStatusFrame.h"
#import "HJZUser.h"
#import "HJZStatus.h"
#import "HJZPhotosView.h"
#import "RCLabel.h"
#import "HtmlString.h"

@implementation HJZStatusFrame


- (void)setStatus:(HJZStatus *)status {
    _status = status;
    
    HJZUser *user = status.user;
    
    // cell宽度
    CGFloat cellW = kWidth - HJZStatusPadding;
    
    // 原创微博父控件
    CGFloat originalW = cellW;
    CGFloat originalX = 0;
    CGFloat originalY = 0;
    
    // 头像
    CGFloat iconXY = HJZStatusPadding;
    CGFloat iconWH = 34;
    _iconViewF = CGRectMake(iconXY, iconXY, iconWH, iconWH);
    

    // 昵称
    CGFloat nameX = CGRectGetMaxX(_iconViewF) + HJZStatusPadding;
    CGFloat nameY = iconXY;
    NSDictionary *nameAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:HJZStatusNameFont]};
    CGSize nameSize = [user.name sizeWithAttributes:nameAttributes];
    
    _nameBtnF = (CGRect){nameX, nameY, nameSize};
    
    //  会员
    if (user.mbtype > 0) {
        CGFloat vipX = CGRectGetMaxX(_nameBtnF) + HJZStatusPadding;
        CGFloat vipY = nameY;
        CGFloat vipW = 14;
        CGFloat vipH = nameSize.height;
        _vipViewF = CGRectMake(vipX, vipY, vipW, vipH);
    }
    
    // 时间
    CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(_nameBtnF) + HJZStatusPadding * 0.5;
    NSDictionary *timeAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:HJZStatusNameFont]};
    CGSize timeSize = [status.created_at  sizeWithAttributes:timeAttributes];
    _timeLabelF = (CGRect){timeX, timeY, timeSize};
    
    // 来源
    CGFloat sourceX = CGRectGetMaxX(_timeLabelF) + HJZStatusPadding * 0.5;
    CGFloat sourceY = timeY;
    NSDictionary *sourceAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:HJZStatusSourceFont]};
    CGSize sourceSize = [status.source sizeWithAttributes:sourceAttributes];
    _sourceLabelF = (CGRect){sourceX, sourceY, sourceSize};
    
    // 正文
    CGFloat contentX = iconXY;
    CGFloat contentY = MAX(CGRectGetMaxY(_iconViewF), CGRectGetMaxY(_timeLabelF)) + HJZStatusPadding;
    CGFloat contentMaxW = cellW - 2 * HJZStatusPadding;
    RCLabel *contentLabel = [[RCLabel alloc] initWithFrame:CGRectMake(contentX, contentY, contentMaxW, 100)];
    contentLabel.font = [UIFont systemFontOfSize:HJZStatusContentFont];
    NSString *str = [HtmlString transformString:status.text];
    contentLabel.componentsAndPlainText = [RCLabel extractTextStyle:str];
    CGSize contentSize = [contentLabel optimumSize:YES];
     _contentLabelF = (CGRect){contentX, contentY, contentSize};
    CGFloat originalH = CGRectGetMaxY(_contentLabelF) + HJZStatusPadding;
    
    // 配图
    if (status.pic_urls.count) {
        CGFloat photoX = iconXY;
        CGFloat photoY = CGRectGetMaxY(_contentLabelF) + HJZStatusPadding;
        CGSize photoSize = [HJZPhotosView sizeWithPhotosCount:(int)status.pic_urls.count];
        _photoViewF = CGRectMake(photoX, photoY, photoSize.width, photoSize.height);
        
        originalH = CGRectGetMaxY(_photoViewF) + HJZStatusPadding;
    }
    
    // 转发微博
    if (status.retweeted_status) {
        // 父控件
        CGFloat retweetX = iconXY;
        CGFloat retweetY = CGRectGetMaxY(_contentLabelF) + HJZStatusPadding * 0.8;
        CGFloat retweetW = contentMaxW;
        
        
        
        NSString *retContentString = [NSString stringWithFormat:@"@%@:%@",  status.retweeted_status.user.name, status.retweeted_status.text];
        // 正文
        CGFloat retContentX = HJZStatusPadding;
        CGFloat retContentY =  HJZStatusPadding;
        CGFloat retContentMaxW = retweetW - 2 * HJZStatusPadding;
        RCLabel *retContentLabel = [[RCLabel alloc] initWithFrame:CGRectMake(retContentX, retContentY, retContentMaxW, 100)];
        retContentLabel.font = [UIFont systemFontOfSize:HJZStatusContentFont];
        NSString *retStr = [HtmlString transformString:retContentString];
        retContentLabel.componentsAndPlainText = [RCLabel extractTextStyle:retStr];
        CGSize retContentSize = [retContentLabel optimumSize:YES];
        _retweetContentLabelF = (CGRect){retContentX, retContentY, retContentSize};
        
        CGFloat retweetH = CGRectGetMaxY(_retweetContentLabelF) + HJZStatusPadding;
        
        // 配图
        if (status.retweeted_status.pic_urls.count) {
            CGFloat retPhotoX = retContentX;
            CGFloat retPhotoY = CGRectGetMaxY(_retweetContentLabelF) + HJZStatusPadding;
            CGSize retPhotoSize = [HJZPhotosView sizeWithPhotosCount:(int)status.retweeted_status.pic_urls.count];
            _retweetPhotoViewF = (CGRect){retPhotoX, retPhotoY, retPhotoSize.width, retPhotoSize.height};
            
            retweetH = CGRectGetMaxY(_retweetPhotoViewF) + HJZStatusPadding;
        }
        
        _retweetViewF = CGRectMake(retweetX, retweetY, retweetW, retweetH);
        
        originalH = CGRectGetMaxY(_retweetViewF) + HJZStatusPadding;
    }
    
    _originalViewF = CGRectMake(originalX, originalY, originalW, originalH);
    
    // 微博工具条
    CGFloat toolX = originalX;
    CGFloat toolY = originalH;
    CGFloat toolW = originalW;
    CGFloat toolH = 35;
    _statusToolBarF = CGRectMake(toolX, toolY, toolW, toolH);
    
    // 转发
    
    // 评论
    
    // 表态
    
    
    // cell的高度
    _cellHeight = CGRectGetMaxY(_statusToolBarF) + HJZStatusPadding * 0.5;
    

    
}
@end
